﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2011-11-23 20:26:09Z</CreationDate>
</File>
*/
#endregion

using System.Windows;
using System.Windows.Controls;

namespace Outcoder.UI.Xaml
{
	/// <summary>
	/// This class selects a template using the type name of the content.
	/// Use "Default" as a template name to allow the selector to locate and default template.
	/// </summary>
	public class TemplateSelectorControl : ContentControl
	{
		protected override void OnContentChanged(object oldContent, object newContent)
		{
			DataTemplate template = GetDefaultContentTemplate();
			if (newContent != null)
			{
				string templateName = newContent.GetType().Name;

				var templateTemp = (DataTemplate)Resources[templateName];
				if (templateTemp == null)
				{
					templateTemp = (DataTemplate)Resources["Default"];
					if (templateTemp == null)
					{
						templateTemp = (DataTemplate)Resources["default"];
					}
				}
				template = templateTemp;
			}

			ContentTemplate = template;
		}

		protected virtual DataTemplate GetDefaultContentTemplate()
		{
			return null;
		}
	}
}
